<?php

namespace Mobile\Controller;

class ArticleController extends CommonController
{
    /**
     * 文章列表
     */
    public function lists()
    {
        $this->assign('pagemark', 'article');
        $this->display();
    }

    /**
     * 文章详情
     */
    public function view()
    {
        $this->assign('pagemark', 'article');
        $this->display();
    }

    /**
     * ajax文章列表
     */
    public function ajax_article_list()
    {
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $html = $this->fetch('ajax_article_list');
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html,
            'loadurl' => query_url('ajax_article_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 提交文章
     */
    public function ajax_article_add()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    /**
     * 提交文章保存
     */
    public function ajax_article_save()
    {
        $this->check_login();
        $data = I('post.');
        $uid = session('ytata.uid');
        $ret = D('Article')->article_add($data, $uid);
        if (!$ret['status']) {
            $this->ajaxReturn(0, $ret['content']);
        }
        $this->ajaxReturn(1, $ret['content']);
    }

    /**
     * 文章点赞
     */
    public function ajax_article_praise()
    {
        $this->check_login();
        $aid = I('get.aid', '', 'intval');
        $uid = session('ytata.uid');
        // 点赞
        $ret = D('ArticlePraise')->article_praise($aid, $uid);
        if ($ret['status']) {
            $this->ajaxReturn(1, $ret['content'], $ret['count']);
        } else {
            $this->ajaxReturn(0, $ret['content']);
        }
    }
}